// 
//  global.js
//  firefox
//  
//  Created by Zak on 2008-06-12.
//  Copyright 2008 Ant.com. All rights reserved.
// 

//Firefox Utils
var _CI = Components.interfaces;
var _CC = Components.classes;

/**
 * Helpers to acces Mozilla Interfaces, Classes and Services
 */
top.CC = function(cName)
{
    return _CC[cName];
};

top.CI = function(ifaceName)
{
    return _CI[ifaceName];
};

top.CCSV = function(cName, ifaceName)
{
    return _CC[cName].getService(_CI[ifaceName]);    
};

top.CCIN = function(cName, ifaceName)
{
    return _CC[cName].createInstance(_CI[ifaceName]);
};

top.QI = function(obj, iface)
{
    return obj.QueryInterface(iface);
};

top.GI = function (obj, iface)
{
    try { return obj.getInterface(iface); }
    catch (e) { if (e.name == "NS_NOINTERFACE") {} }
    return null;
};

// Get access to mozilla interfaces (see http://developer.mozilla.org/en/docs/Interfaces for more informations)
const consoleService 	= CCSV("@mozilla.org/consoleservice;1", "nsIConsoleService");
const appStartup 		= CCSV("@mozilla.org/toolkit/app-startup;1", "nsIAppStartup");
const wbp 				= CCSV('@mozilla.org/embedding/browser/nsWebBrowserPersist;1', "nsIWebBrowserPersist");
const observerService 	= CCSV("@mozilla.org/observer-service;1", "nsIObserverService");
const dlMgr 			= CCSV('@mozilla.org/download-manager;1', "nsIDownloadManager");
const ioService 		= CCSV("@mozilla.org/network/io-service;1", "nsIIOService");
const dlMgrUI 			= CCIN("@mozilla.org/filepicker;1", "nsIFilePicker");
const wm 				= CCSV("@mozilla.org/appshell/window-mediator;1", "nsIWindowMediator");
const clipboardHelper   = CCSV("@mozilla.org/widget/clipboardhelper;1", "nsIClipboardHelper");


/**
 * Did we clean the search box already ?
 */
var cleanedSearchBox = false;

/**
 * Abstract for the getElementById function
 * @param id            The id to look for
 * @param doc           (Optional) Search in the specified scope instead of document
 */
function $(id, doc)
{
    if (doc == undefined)
        return document.getElementById(id);
    else
        return doc.getElementById(id);
}

/**
 * Restart Firefox
 */
function re()
{
    appStartup.quit(appStartup.eAttemptQuit | appStartup.eRestart);
}

/**
 * Inspect the current object
 */
top.inspect = function (obj)
{
    var r = "";
    
	try
	{
		for (i in obj)
		{	
			try
			{
				r += i + " = " + obj[i] + "\n";
			}
			catch (e)
			{
				r += "ERROR WHILE GETTING ELEMENT \"" + i + "\"\n";
			}
		}
	}
	catch (e)
	{
		r += "ERROR CANNOT INPECT OBJECT\n";
	}
	
    return r;
}

/**
 * Trim a string
 */
String.prototype.trim = function ()
{
    var r = this;
    
    if (!r)
        return "";
    r = r.replace(/^\s+/, '');
    r = r.replace(/\s+$/, '');
    r = r.replace(/\s+/g, ' ');
    
    return r;
}

/**
 * Sanitize a string
 */
String.prototype.sanitize = function ()
{
    return this.replace(/[^a-z0-9_ -]/gi, '').replace(/[ _]+/g, "_");;
}

String.prototype.upFirstLetter = function ()
{
    return this.substr(0, 1).toUpperCase() + this.substr(1);
}


/**
 * Safely get a property using try/catch block
 * @param obj           The object to get the property from
 * @param prop          The property to get
 * @return property     The value or an empty string
 */
function safeGet(obj, prop)
{
	var property;

	try
	{
		property = obj[prop];
	} 
	catch (e)
	{
		property = "";
	}

	return property;
}


